<script>
import { formMixin } from '@/mixins/index'
import { stateOptions } from '@/dicts/index'
import { mergeObjectAssignments } from '@/utils/index'
import { addOrUpdateWarningApi } from '@/api/footprint'
export default {
  name: 'AddOrUpdate',
  mixins: [formMixin],
  data() {
    return {
      dialogVisible: false,
      form: {
        id: null,
        name: '',
        address: '',
        power: '',
        voltage: '',
        state: '',
        remark: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入检测点名称', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入检测点地址', trigger: 'blur' }
        ],
        power: [
          { required: true, message: '请输入实时功率', trigger: 'blur' }
        ],
        voltage: [
          { required: true, message: '请输入实时电压', trigger: 'blur' }
        ],
        state: [
          { required: true, message: '请选择状态', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    getStateOptions() {
      return stateOptions
    }
  },
  methods: {
    /**
     * 初始化
     * @param {Object | null} data
     */
    init(data = null) {
      this.dialogVisible = true
      if (data) mergeObjectAssignments(this.form, data)
    },
    /**
    * 提交, 验证
    */
    submit() {
      this.submitForm('formRef').then(() => {
        const form = JSON.parse(JSON.stringify(this.form))
        if (!form.id) delete form.id
        addOrUpdateWarningApi(form).then(() => {
          this.$message.success(`${form.id ? '修改' : '新增'}成功`)
          this.$emit('updateData')
        })
        this.closePopup()
      }, () => {
        this.$message.warning('数据验证失败')
      })
    },
    /**
     * 关闭，重置数据
     */
    closePopup() {
      this.resetForm('formRef')
      this.dialogVisible = false
    }
  }
}
</script>
<template>
  <el-dialog :visible.sync="dialogVisible" :title="`${form.id? '修改' : '新增'}能效检测点`" @close="closePopup">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="检测点名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="检测点地址" prop="address">
        <el-input v-model="form.address" />
      </el-form-item>
      <el-form-item label="实时功率" prop="power">
        <el-input v-model="form.power" />
      </el-form-item>
      <el-form-item label="实时电压" prop="voltage">
        <el-input v-model="form.voltage" />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="form.state">
          <el-option v-for="item in getStateOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closePopup">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>
